// 1. 引入mongoose模块并配置
const mongoose = require("mongoose");

const moment = require("moment");
moment.locale("zh-cn");

// 配置mongoose，取消自动将集合名称变为复数形式
// 默认情况下，mongoose会将定义的模型名称自动转换为复数形式作为集合名称
mongoose.pluralize(null);

const { Schema, model } = mongoose;

// 配置mongoose，取消在文档中自动添加版本键（__v）
// 默认情况下，mongoose会在每个文档中添加一个版本键，用于处理内部版本控制
const versionKey = { versionKey: false };

// 2. 定义用户账号表的Schema
const UserSchema = new Schema(
  {
    // 使用混合类型，可以存储任何类型的数据，但不会进行类型校验
    //   name: Schema.Types.Mixed,
    mobile: {
      type: String,
      unique: true, //唯一性
      trim: true,
    },
    password: {
      type: String,
      trim: true,
      select: false, //不返回给前端
      default: "", //默认值
    },
    uid: {
      // 用户唯一标识
      type: String,
      unique: true,
      default: () => new Date().getTime(),
    },
    avatarUrl: {
      //头像
      type: String,
      default:
        "https://gd-hbimg.huaban.com/d7d4574c9364e191d816322a6e9788f8edd2c7a8527f-DDR4Ai_fw1200webp",
    },
    nickname: {
      // 昵称
      type: String,
      default: "新用户",
    },
    gender: {
      // 性别
      type: String,
      default: 0,
    },
    birthday: {
      // 生日
      type: String,
      default: "1999-01-01",
    },
    age: {
      // 年龄
      type: Number,
      default: "",
    },
    city: {
      type: String,
      default: "",
    },
    backdrop: {
      // 背景
      type: String,
      default: "https://img.shetu66.com/2023/06/25/1687662757639430.png",
    },
    my_tags: {
      // 用户标签
      type: Array,
      default: [],
    },
  },
  versionKey
);

//用户发表的游记表
const ArticleSchema = new Schema(
  {
    author_uid: String, //文章作者uid
    author_id: { type: mongoose.Types.ObjectId, ref: "user", required: true }, //关联用户表
    title: {
      //游记标题
      type: String,
      required: true,
      trim: true,
    },
    content: {
      //文章内容
      type: String,
      required: true,
    },
    cover_image: {
      //封面图片
      type: Object,
      width: Number,
      height: Number,
    },
    image: {
      //图片集合
      type: Array,
      default: [],
    },
    videoUrl: {
      //短视频链接
      url: {
        type: String,
        default: "",
      },
      width: {
        type: Number,
        default: 0,
      },
      height: {
        type: Number,
        default: 0,
      },
    },
    fileType: {
      //判断文件类型:image:'图片'，video:'视频'
      type: String,
      default: "image",
    },
    city: String, //城市
    address: String, //详细地址
    province: String, //省份
    tag: {
      type: Array,
      default: [],
    },
    time: {
      //发表时候
      type: String,
      default: () => moment().utcOffset(8).format("YYYY-MM-DD HH:mm:ss"), //自动获取时间
    },
    time_stamp: {
      //发表时间戳
      type: Number,
      default: () => moment().unix(), //自动获取时间戳
    },
  },
  versionKey
);

//小程序端首页10个游记分类
const HometabSchema = new Schema(
  {
    icon: String,
    text: String,
  },
  versionKey
);

//点赞表
const LikeSchema = new Schema(
  {
    user_uid: String, //点赞用户uid
    author_uid: String, //作者uid
    user_id: {
      type: mongoose.Types.ObjectId,
      ref: "user",
      require: true,
    },
    article_id: {
      type: mongoose.Types.ObjectId,
      ref: "article",
      require: true,
    },
  },
  versionKey
);

//收藏表
const collectionSchema = new Schema(
  {
    user_uid: String, //收藏用户uid
    author_uid: String, //作者uid
    user_id: {
      type: mongoose.Types.ObjectId,
      ref: "user",
      require: true,
    },
    article_id: {
      type: mongoose.Types.ObjectId,
      ref: "article",
      require: true,
    },
    time: {
      //收藏时间
      type: String,
      default: () => moment().utcOffset(8).format("YYYY-MM-DD HH:mm:ss"), //自动获取时间
    },
  },
  versionKey
);

//评论表
const commentSchema = new Schema(
  {
    user_uid: String, //评论用户uid
    author_uid: String, //作者uid
    user_id: {
      type: mongoose.Types.ObjectId,
      ref: "user",
      require: true,
    },
    article_id: {
      type: mongoose.Types.ObjectId,
      ref: "article",
      require: true,
    },
    time: {
      //收藏时间
      type: String,
      default: () => moment().utcOffset(8).format("YYYY-MM-DD HH:mm:ss"), //自动获取时间
    },
    content: {
      type: String,
      trim: true,
      default: "",
    },
  },
  versionKey
);

//关注表
const ConcernSchema = new Schema(
  {
    user_uid: String, //A关注B,A用户的uid
    im_concerned_uid: String, //A关注B , B用户的uid
  },
  versionKey
);

//目的地：当地玩法
const LocalWaySchema = new Schema(
  {
    image: String,
    categories: String,
    color: String,
  },
  versionKey
);
//目的地界面，左边国家分类
const CountrySchema = new Schema(
  {
    country: String,
    type: String,
  },
  versionKey
);
//目的地界面，右边国家
const CitySchema = new Schema(
  {
    image: String,
    city: String,
    type: String,
  },
  versionKey
);

//发起结伴表
const CompaniosSchema = new Schema(
  {
    uid: String, //发起人的uid
    description: String, //描述
    image: Object, //封面图片
    city: String, //城市
    full_address: String, //目的地详细地址
    companion_time: String, //结伴时间
    companion_timestamp: Number, //结伴时间戳
    number_of_people: Number, //人数
    time: {
      type: String,
      default: () => moment().utcOffset(8).format("YYYY-MM-DD"), //自动获取时间
    },
    timestamp: {
      type: Number,
      default: () => moment().unix(), //自动获取时间戳
    },
  },
  versionKey
);

//报名表
const SignUpSchema = new Schema(
  {
    //关联发起结伴表
    singup_id: {
      type: mongoose.Types.ObjectId,
      ref: "companios",
      require: true,
    },
    user_uid: String, //报名人uid
    contact_inform: String, //联系方式
    gender: Number, //性别 0:女 1:男
    introduce: String, //自我介绍
    time: {
      type: String,
      default: () => moment().utcOffset(8).format("YYYY-MM-DD"), //自动获取时间
    },
    timestamp: {
      type: Number,
      default: () => moment().unix(), //自动获取时间戳
    },
  },
  versionKey
);
// 3. 导出用户模型
// 使用mongoose的model方法创建一个模型，第一个参数是集合名称（单数形式），第二个参数是Schema
// 这里导出的modelUser可以在其他文件中被require并使用，用于操作用户集合
module.exports = {
  modelUser: model("user", UserSchema),
  modelArticle: model("article", ArticleSchema),
  modelHometab: model("hometab", HometabSchema),
  modelLike: model("like", LikeSchema),
  modelCollection: model("collection", collectionSchema),
  modelComment: model("comment", commentSchema),
  modelConcern: model("concern", ConcernSchema),
  modelLocalWay: model("localway", LocalWaySchema),
  modelCountry: model("country", CountrySchema),
  modelCity: model("city", CitySchema),
  modelCompanios: model("companios", CompaniosSchema),
  modelSignUp: model("signup", SignUpSchema),
};
